package com.forum.controller;

import com.forum.pojo.User;
import com.forum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @Author Mirror
 * @CreateDate 2020/3/22.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 根据用户ID查询用户信息
     * @param uid
     * @return
     */
    @GetMapping("/{uid}")
    public ResponseEntity<User> queryUserByUid(@PathVariable(name = "uid")Long uid){
        User user = this.userService.queryUserByUid(uid);
        if (user == null){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(user);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(User user) {
        Boolean boo = this.userService.register(user);
        if (boo == null || !boo) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
            return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 用户登陆
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<User> userLogin(String username, String password){
        User user = this.userService.userLogin(username,password);
        if (user == null){
            ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(user);
    }
}
